//
//  TReleaseFindPassengerViewController.m
//  LoveShare
//
//  Created by ways on 2017/4/26.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TReleaseFindPassengerViewController.h"

#import "TFindDriverListViewController.h"
#import "TPickerView.h"

#import "TPassengerViewModel.h"

@interface TReleaseFindPassengerViewController ()

@property (strong, nonatomic) TPassengerViewModel *passengerViewModel;

@property (strong, nonatomic) NSDate *selectedDate;

@property (strong, nonatomic) IBOutlet UITextField *startTextField;
@property (strong, nonatomic) IBOutlet UITextField *endTextField;
@property (strong, nonatomic) IBOutlet UITextField *timeTextField;
@property (strong, nonatomic) IBOutlet UITextField *passengerNumTextField;
@property (strong, nonatomic) IBOutlet UITextField *messageTextField;
@property (strong, nonatomic) IBOutlet UITextField *priceTextField;

@property (strong, nonatomic) IBOutlet UIButton *releaseButton;

@property (nonatomic, strong) TPickerView *pickerView;

@end

@implementation TReleaseFindPassengerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"找乘客";
    
    [self initProperty];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark - init

- (void)initProperty {
    
    [self initNavigationBarItem];
    
    [self.startTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        
    }];
    
    [self.endTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        
    }];
    
    
    [SVProgressHUD setInfoImage:nil];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    @weakify(self)
    [[self.releaseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self releaseButtonAction];
    }];
    
}


- (TPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[TPickerView alloc] initWithFrame:CGRectMake(40, (TScreenHeight - 44 - 20 - 300)/2, TScreenWidth - 40*2, 300) type:TPickerViewTypeDatePicker];
        _pickerView.isShowTopControlView = YES;
        _pickerView.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        TWeakSelf(self)
        _pickerView.selectRowBlock = ^(NSDate *selectedDate){
            weakself.selectedDate = selectedDate;
            NSString *dateString = [selectedDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
            NSLog(@"%@",dateString);
            weakself.timeTextField.text = dateString;
        };
        [self.view addSubview:_pickerView];
    }
    return _pickerView;
}


- (void)initNavigationBarItem {
    
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"发布记录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClickAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (TPassengerViewModel *)passengerViewModel {
    if (!_passengerViewModel) {
        _passengerViewModel = [[TPassengerViewModel alloc] initPassengerViewModel];
        _passengerViewModel.requestURL = T_PublishFindPassenger;
    }
    return _passengerViewModel;
}


#pragma mark - submit

- (void)releaseButtonAction {
    if ([self.startTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入起始地"];
        [SVProgressHUD dismissWithDelay:1.0f];
    } else if ([self.endTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入目的地"];
        [SVProgressHUD dismissWithDelay:1.0f];
    } else if ([self.timeTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入出发时间"];
        [SVProgressHUD dismissWithDelay:1.0f];
    } else if ([self.passengerNumTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入乘客数量"];
        [SVProgressHUD dismissWithDelay:1.0f];
    }  else if ([self.messageTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入备注"];
        [SVProgressHUD dismissWithDelay:1.0f];
    } else if ([self.priceTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入联系电话"];
        [SVProgressHUD dismissWithDelay:1.0f];
    } else {
        [self.view endEditing:YES];
        
        self.passengerViewModel.releaseFindPassengerModel.startPlace = self.startTextField.text;
        self.passengerViewModel.releaseFindPassengerModel.destination = self.endTextField.text;
        NSString *goTime = [@(self.selectedDate.timeIntervalSince1970) stringValue];
        self.passengerViewModel.releaseFindPassengerModel.goTime = [goTime numberValue];
        self.passengerViewModel.releaseFindPassengerModel.seat = self.passengerNumTextField.text;
        self.passengerViewModel.releaseFindPassengerModel.message = self.messageTextField.text;
        self.passengerViewModel.releaseFindPassengerModel.contactMobile = self.priceTextField.text;
        @weakify(self)
        [[self.passengerViewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
            @strongify(self)
            if (self.releaseSuccessedBlock) {
                self.releaseSuccessedBlock();
            }
            TFindDriverListViewController *driverList = [[TFindDriverListViewController alloc] init];
            driverList.isReleaseFindPassengerSuccessedComing = YES;
            [self.navigationController pushViewController:driverList animated:YES];
        }];
    }
}

#pragma mark - Actions Method

- (void)rightBarItemClickAction {
    TFindDriverListViewController *driverList = [[TFindDriverListViewController alloc] init];
    driverList.isReleaseFindPassengerComing = YES;
    [self.navigationController pushViewController:driverList animated:YES];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.timeTextField) {
        [self.view endEditing:YES];
        [self.pickerView controllerTPickerViewVisibility:YES];
        return NO;
    }
    [self.pickerView controllerTPickerViewVisibility:NO];
    return YES;
}

@end
